# -*- coding: utf-8 -*-

from mon.baselib.utils import OsUtils
from mon.baselib.utils import StringUtils

from src.handler.AbstractHandler import AbstractHandler


def do_kill(args):
    if args.pid > 0:
        OsUtils.kill_process_by_pid(args.pid)
    elif args.port > 0:
        OsUtils.kill_process_by_port(args.port)
    pass


def do_find(args):
    if StringUtils.not_empty(args.name):
        exe_name = OsUtils.exe_name()
        print(f"Exclude: {exe_name}")
        processes = OsUtils.find_process_by_process_name(args.name, exe_name)
        for process in processes:
            print(process)
        pass


def do_info(args):
    if args.pid > 0:
        process_info = OsUtils.get_process_by_pid(args.pid)
        if process_info:
            print(f"Process Info (PID {args.pid}): {process_info}")
        else:
            print(f"No process found with PID {args.pid}")
    elif args.port > 0:
        process_info = OsUtils.get_process_by_port(args.port)
        if process_info:
            print(f"Process Info (Port {args.port}): {process_info}")
        else:
            print(f"No process found using port {args.port}")


class ProcessHandler(AbstractHandler):

    def _setup_arg(self, subparsers):
        process_parser = subparsers.add_parser('process', help='进程管理')
        process_subparsers = process_parser.add_subparsers(dest="process_command", help="进程管理子命令")

        # 杀死进程的子命令
        kill_parser = process_subparsers.add_parser('kill', help='根据进程 ID 或端口号杀死进程')
        kill_group = kill_parser.add_mutually_exclusive_group(required=True)
        kill_group.add_argument('--pid', '-p', type=int, default=0, help='进程ID')
        kill_group.add_argument('--port', '-P', type=int, default=0, help='端口号')
        kill_parser.set_defaults(func=self.do_process)

        # 获取进程信息的子命令
        info_parser = process_subparsers.add_parser('info', help='根据进程ID或端口号获取进程信息')
        info_group = info_parser.add_mutually_exclusive_group(required=True)
        info_group.add_argument('--pid', '-p', type=int, default=0, help='进程ID')
        info_group.add_argument('--port', '-P', type=int, default=0, help='端口号')
        info_parser.set_defaults(func=self.do_process)

        # 获取进程信息的子命令
        find_parser = process_subparsers.add_parser('find', help='根据进程名获取进程信息')
        find_group = find_parser.add_mutually_exclusive_group(required=True)
        find_group.add_argument('--name', '-n', type=str, default='', help='进程名')
        find_parser.set_defaults(func=self.do_process)

    def _check(self, args):
        """检查参数是否有效"""
        if args.process_command == 'kill':
            return args.pid > 0 or args.port > 0
        elif args.process_command == 'find':
            return StringUtils.not_empty(args.name)
        elif args.process_command == 'info':
            return args.pid > 0 or args.port > 0
        return False

    def _process(self, args):
        if args.process_command == 'kill':
            do_kill(args)
        elif args.process_command == 'find':
            do_find(args)
        elif args.process_command == 'info':
            do_info(args)
